package com.yjck.ijpay.controller;

import com.yjck.ijpay.pojo.BaseResponse;
import com.yjck.ijpay.pojo.XhMember;
import com.yjck.ijpay.service.IPayService;
import com.yjck.ijpay.utils.WeChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Copyright (C), 2015-2021,鹰击长空科技服务(苏州)有限公司
 * @Author: lyz
 * @Date: 2021/3/17 14:22
 * @Description: 支付宝支付/微信支付
 * History: //修改记录
 * <author> <time> <version> <desc>
 * 修改人姓名 修改时间 版本号   描述
 */
@RestController
@RequestMapping
public class PayController {

    @Autowired
    private IPayService payService;

    //  TODO  要押金流水 改变状态为 3-支付失败  时长定位5分钟  走redis延时队列


    /**
     * @param orderNo 订单id
     * @return com.xingheng.common.pojo.BaseResponse
     * @Description: 根据订单号查询微信服务端是否支付完成
     * @author lyz
     * @version [1.0, 2021-04-27]
     */
    @GetMapping("/pay/")
    public BaseResponse pay(@RequestParam("id") String orderNo) {
        return BaseResponse.success(WeChatUtil.weChatTransactionsOutTradeNo(orderNo));
    }

    /**
     * 创建微信支付异步回调接口
     *
     * @param request
     * @param response
     */
    @PostMapping("/payNotify")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) {
        payService.payNotify(request, response);
    }

    /**
     * @return java.lang.String
     * @Description: 微信获取平台证书接口
     * @author lyz
     * @version [1.0, 2021-04-27]
     */
    @GetMapping("/pay/v3")
    public String v3Get() {
        return payService.v3Get();
    }

    /**
     * @return java.lang.String
     * @Description: 微信退款接口
     * @author lyz
     * @version [1.0, 2021-04-27]
     */
    @GetMapping("/refund")
//    public BaseResponse refund(@LoginUser XhMember xhMember) {
    public BaseResponse refund(XhMember xhMember) {
        return payService.refund(xhMember.getId());
    }


    /**
     * 押金退款异步回调接口
     *
     * @param request
     * @param response
     */
    @PostMapping("/refundNotify")
    public void refundNotify(HttpServletRequest request, HttpServletResponse response) {
        payService.refundNotify(request, response);
    }


    /**
     * @return com.xingheng.common.pojo.BaseResponse
     * @Description: 创建支付分订单
     * @author lyz
     * @version [1.0, 2021-05-06]
     */
    @PostMapping("/payscore")
    public BaseResponse payScore() {
        payService.payScore();
        return BaseResponse.success();
    }

    /**
     * @param request
     * @param response
     * @return void
     * @Description: 创建微信支付分回调
     * @author lyz
     * @version [1.0, 2021-05-06]
     */
    @PostMapping("/payScoreNotify")
    public void payScoreNotify(HttpServletRequest request, HttpServletResponse response) {
        payService.payScoreNotify(request, response);
    }
}
